package com.cgtong.android.mvp.presenter;


import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.bean.NewsBean;
import com.cgtong.android.mvp.contract.CityDynamicContract;
import com.cgtong.android.mvp.model.CityDynamicModel;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Created by Sky.Bing on 2017/10/18.
 */

public class CityDynamicPresenter implements CityDynamicContract.Presenter {
    private CityDynamicContract.View cityDynamicView;
    private CityDynamicModel mCityDynamicMode;

    public CityDynamicPresenter(CityDynamicContract.View view) {
        this.cityDynamicView = view;
        mCityDynamicMode = new CityDynamicModel();
    }

    @Override
    public void loadNewsDynamicData(String maxTime) {
        //request network data
        cityDynamicView.setEnableLoadMore(false);
        mCityDynamicMode.getNewsDynamic(maxTime, new SimpleCallBack<List<NewsBean>>() {
            @Override
            public void onError(ApiException e) {
                cityDynamicView.onLoadError();
            }

            @Override
            public void onSuccess(List<NewsBean> newsBeanList) {
                cityDynamicView.setRecyclerViewAdapter(true, newsBeanList);
                cityDynamicView.setEnableLoadMore(true);
                cityDynamicView.setRefreshing(false);
            }
        });
    }

    @Override
    public void loadMore(String maxTime) {
        mCityDynamicMode.getNewsDynamic(maxTime, new SimpleCallBack<List<NewsBean>>() {
            @Override
            public void onError(ApiException e) {
                if (!TextUtils.isEmpty(e.getMessage())){
                   ToastUtils.showLong(e.getMessage());
                   cityDynamicView.onLoadError();
                }
            }

            @Override
            public void onSuccess(List<NewsBean> newsBeanList) {
                cityDynamicView.setRecyclerViewAdapter(false, newsBeanList);
                cityDynamicView.setRefreshing(false);
            }
        });
    }
}